#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QPropertyAnimation>
#include <QDebug>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->musicImageBox == watched)
    {
        int width = ui->musicImageBox->width();
        int height = ui->musicImageBox->height();
        if(event->type() == QEvent::Enter)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(9,10,width,height));
            animation->setEndValue(QRect(9,0,width,height));
            animation->start();
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
        }
        else if(event->type() == QEvent::Leave)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(9,0,width,height));
            animation->setEndValue(QRect(9,10,width,height));
            animation->start();
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
        }
        return true;
    }
    return QObject::eventFilter(watched,event);
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(const QString &Imagepath)
{
    QString imgStyle = "border-image:url("+Imagepath+");";
    ui->recMusicImage->setStyleSheet(imgStyle);
}
